package com.longjianghuasheng.logistics.business.address

import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.entity.AddressResponse
import com.longjianghuasheng.logistics.entity.AreaResponse
import com.longjianghuasheng.logistics.ext.commit
import com.longjianghuasheng.logistics.ext.commit2
import com.longjianghuasheng.logistics.ext.toastNormal
import com.longjianghuasheng.logistics.network.apiService

class AddressViewModel : BaseMultiStateViewModel() {

    var province = 0
    var city = 0
    var area = 0
    var name = ""
    var mobile = ""
    var address = ""
    var is_default = 0
    var id = 0

    val result by lazy {
        MutableLiveData<ArrayList<AddressResponse>>()
    }

    val result1 by lazy {
        MutableLiveData<AddressResponse>()
    }

    val resultArea by lazy {
        MutableLiveData<AreaResponse>()
    }

    val notify by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun requestData() {
        getData({ apiService.address_list() }, result)
    }

    fun requestData(id: Int) {
        getData({ apiService.address_show(id) }, result1)
    }

    fun addAddress() {
        var isChecked = false
        when {
            name.isBlank() -> toastNormal("姓名不能为空")
            mobile.isBlank() -> toastNormal("手机号不能为空")
            address.isBlank() -> toastNormal("请填写详细地址")
            else -> isChecked = true
        }
        if (!isChecked) return
        commit2({
            apiService.address_save(hashMapOf<String, String>().apply {
                if (id != 0) {
                    put("id", id.toString())
                }
                put("names", name)
                put("mobile", mobile)
                put("address", address)
                put("is_default", is_default.toString())
                put("province", province.toString())
                put("city", city.toString())
                put("area", area.toString())
            })
        }, notify)

    }

    fun area(pid: Int) {
        commit({ apiService.area(pid) }, resultArea)
    }

    fun address_default(id: Int) {
        commit2({ apiService.address_default(id) }, notify)
    }


    val deleteNotify by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun address_del() {

        commit2({ apiService.address_del(id) }, deleteNotify)
    }

}